/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.dao.WarehouseDao;
import com.sigmatrix.h5.dto.WarehouseData;
import com.sigmatrix.h5.entity.Warehouse;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.request.Request;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.WarehouseService;
import com.sigmatrix.h5.service.common.impl.BaseServiceImpl;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.vo.WarehouseVo;

/**
 *@ClassName: WarehouseServiceImpl
 *@Description: 仓库业务层接口
 *@author youjun
 *@date 2017-06-24 17:31:11
 */
@Service(value="warehouseService")
public class WarehouseServiceImpl extends BaseServiceImpl<WarehouseData> implements WarehouseService {

	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(WarehouseServiceImpl.class);
	
	/**仓库数据库操作接口*/
	@Autowired
	private transient WarehouseDao warehouseDao;
	
	/**
	 * (non-Javadoc)
	 *@Title: findById
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.WarehouseService#findById(java.lang.Integer, java.lang.String)
	 *@author youjun
	 *@date 2017年6月26日 下午5:02:58
	 */
	@Override
	public WarehouseData findById(final Integer id, final String enterpriseCode) throws ServiceException {
		
		if(id==null) {
			throw new ServiceException("请指定主键");
		}
				
		final WarehouseData data = new WarehouseData();
		try {
			final Warehouse warehouse = warehouseDao.findById(id, enterpriseCode);
			if(warehouse!=null) {
				data.setWarehouseId(warehouse.getId());
				data.setWarehouseCode(warehouse.getCode());
				data.setWarehouseName(warehouse.getName());
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return data;
	}

	/**
	 * (non-Javadoc)
	 *@Title: findListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.WarehouseService#findListByCondition(com.sigmatrix.sc.handheld.inter.web.vo.WarehouseVo)
	 *@author youjun
	 *@date 2017年6月26日 下午5:02:54
	 */
	@Override
	public List<WarehouseData> findListByCondition(final WarehouseVo warehouseVo) throws ServiceException {

		final List<WarehouseData> dataList = new ArrayList<WarehouseData>();
		if(warehouseVo==null||StringUtils.isBlank(warehouseVo.getEnterpriseCode())) {
			throw new ServiceException("参数有误");
		}
		try {
			final Warehouse warehouse = new Warehouse();
			if(warehouseVo!=null) {
				BeanUtils.copyProperties(warehouseVo, warehouse);
			}
			final List<Warehouse> list = warehouseDao.findListByCondition(warehouse, warehouseVo.getEnterpriseCode());
			if(!CollectionUtils.isEmpty(list)) {
				WarehouseData data = null;
				for(final Warehouse item:list) {
					data = new WarehouseData();
					BeanUtils.copyProperties(item, data);
					dataList.add(data);
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return dataList;
	}
	
	/**
	 *@Title: getAllWarehouseMap
	 *@Description: 获得所有的仓库map
	 *@return Map<Integer,WarehouseData>
	 *@author youjun
	 *@date 2017年6月25日 下午2:26:47
	 *@return
	 *@throws ServiceException
	 */
	@Override
	public Map<Integer, WarehouseData> getAllWarehouseMap(final String enterpriseCode) throws ServiceException {
		
		Map<Integer, WarehouseData> warehouseMap = new HashMap<Integer, WarehouseData>();
		final List<WarehouseData> warehouseList = getAllWarehouseList(enterpriseCode);
		if(!CollectionUtils.isEmpty(warehouseList)) {
			for(final WarehouseData data:warehouseList) {
				warehouseMap.put(data.getWarehouseId(), data);
			}
		}
		return warehouseMap;
	}

	/**
	 *@Title: getAllWarehouseList
	 *@Description: 获得所有的仓库列表
	 *@return List<WarehouseData>
	 *@author youjun
	 *@date 2017年6月25日 下午2:26:38
	 *@return
	 * @throws ServiceException 
	 */
	@Override
	public List<WarehouseData> getAllWarehouseList(final String enterpriseCode) throws ServiceException {
		
		List<WarehouseData> warehouseList = new ArrayList<WarehouseData>();
		LOGGER.info("当前企业:{},基础数据中的仓库列表为空,将从数据库中查询信息", enterpriseCode);
		final WarehouseVo warehouseVo = new WarehouseVo();
		warehouseVo.setEnterpriseCode(enterpriseCode);
		warehouseList = findListByCondition(warehouseVo);
		LOGGER.info("当前企业:{},查询的所有仓库列表为:{}", enterpriseCode, JSONObject.toJSONString(warehouseList));
		return warehouseList;
	}
	
	/**
	 * (non-Javadoc)
	 *@Title: getWarehouseFromMap
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.WarehouseService#getWarehouseFromMap(java.util.Map, java.lang.Integer)
	 *@author youjun
	 *@date 2017年6月26日 下午5:03:07
	 */
	@Override
	public WarehouseData getWarehouseFromMap(final Map<Integer, WarehouseData> warehouseMap, final Integer warehouseId, 
			final String enterpriseCode) throws ServiceException {
		
		if(warehouseMap==null) {
			throw new ServiceException("map不能为null");
		}
		if(warehouseId==null || warehouseId.intValue()==Cons.NULL_PRIMARY_KEY_VALUE) {
			return null;
		}
		if(warehouseMap.containsKey(warehouseId)) {
			return warehouseMap.get(warehouseId);
		}
		final WarehouseData data = findById(warehouseId, enterpriseCode);
		warehouseMap.put(warehouseId, data); // 放入map中
		return data;
	}

	/**(non-Javadoc)
	 *@Title: syncList
	 *@Description: 同步仓库列表
	 *@see com.sigmatrix.sc.handheld.inter.service.WarehouseService#syncList(com.sigmatrix.sc.handheld.inter.web.vo.WarehouseVo)
	 *@author youjun
	 *@date 2017年6月27日 上午10:43:25
	 */
	@Override
	public Response syncList(final WarehouseVo warehouseVo) throws ServiceException {

		try{
			// 验证参数
			final Response validateResponse = validateParams(warehouseVo);
			LOGGER.info("参数验证结果:{}", JSONObject.toJSONString(validateResponse));
			if(validateResponse.getCode().intValue()!=Cons.RESPONSE_CODE_SUCCESS) {
				return validateResponse;
			}
			final List<WarehouseData> warehouseList = findPartColumnListByCondition(warehouseVo);
			LOGGER.info("同步仓库列表接口查询的仓库列表为:", JSONObject.toJSONString(warehouseList));
			LOGGER.info("替换基础数据中的仓库列表和仓库map为同步接口的查询结果");
			return new Response(ResponseEnum.SUCCESS, warehouseList);
		} catch (Exception e) {
			LOGGER.error("syncList fail", e);
			throw new ServiceException(e);
		}
	}

	/**
	 *@Title: findPartColumnListByCondition
	 *@Description: TODO()
	 *@return List<WarehouseData>
	 *@author youjun
	 *@date 2017年6月27日 上午11:22:18
	 *@param warehouseVo
	 *@return
	 * @throws ServiceException 
	 */
	private List<WarehouseData> findPartColumnListByCondition(final WarehouseVo warehouseVo) throws ServiceException {
		
		try{
			final Warehouse warehouse = new Warehouse();
			if(warehouseVo!=null) {
				BeanUtils.copyProperties(warehouseVo, warehouse);
			}
			return warehouseDao.findPartColumnListByCondition(warehouse, warehouseVo.getEnterpriseCode());
		}catch(DaoException e){
			throw new ServiceException(e);
		}
	}

	/**
	 *@Title: validateParams
	 *@Description: TODO()
	 *@return Response
	 *@author youjun
	 *@date 2017年6月27日 上午11:20:31
	 *@param warehouseVo
	 *@return
	 */
	private Response validateParams(WarehouseVo warehouseVo) {
		return validateBasicParams(warehouseVo);
	}
	
	
	/**(non-Javadoc)
	 *@Title: syncList
	 *@Description: 同步仓库列表
	 *@see com.sigmatrix.h5.service.WarehouseService#syncList(com.sigmatrix.h5.web.vo.WarehouseVo)
	 *@author youjun
	 *@date 2017年6月27日 上午10:43:25
	 */
	@Override
	public Response syncList(final Request reques) throws ServiceException {
		try{
			// 验证参数
			final Response validateResponse = validateBasicParams(reques);
			if(validateResponse.getCode().intValue()!=Cons.RESPONSE_CODE_SUCCESS) {
				LOGGER.info("参数验证结果:{}", JSONObject.toJSONString(validateResponse));
				return validateResponse;
			}
			final List<WarehouseData> warehouseList = 
					warehouseDao.findPartByDealerId(reques.getEnterpriseCode(),reques.getUserDealerId());
			return new Response(ResponseEnum.SUCCESS, warehouseList);
		} catch (Exception e) {
			LOGGER.error("syncList fail", e);
			throw new ServiceException(e);
		}
	}

}
